public class Solution1011 {
    public int shipWithinDays(int[] weights, int days) {
        int sum=0,max=0;
        for (int weight : weights) {
            sum+=weight;
            max=Math.max(max,weight);
        }
        int l=max,r=sum;
        int ans=-1;
        while (l<=r){
            int cur=0,record=1;
            int mid=(l+r)/2;
            for (int i=0;i<weights.length;i++){
                if (cur+weights[i]>mid){
                    cur=weights[i];
                    record++;
                }else{
                    cur+=weights[i];
                }
            }
            if (record<=days){
                ans=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1011().shipWithinDays(new int[]{1,2,3,4,5,6,7,8,9,10},5));
    }
}
